package UI;

import Model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/showlist")
public class Showlist extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doPost(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            List<Student> list = (List<Student>) req.getAttribute("list");
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();

            writer.write("<!Doctype html>");
            writer.write("<html>");
            writer.write("<head>");
            writer.write("</head>");
            writer.write("<body style='backgroud:#ccffff;'>");
            writer.write("<h1>学生信息展示</h1>");
            writer.write("<h3><a href='insert'>录入信息</a></h3>");
            writer.write("<table cellpadding='3' cellspacing='0' border='1' align='center'>");
            writer.write("<tr><th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>专业</th><th>时间</th><th>操作1</th><th>操作2</th></tr>");
            for (int i = 0; i < list.size(); i++) {
                writer.write("<tr>");
                writer.write("<td>"+list.get(i).getId()+"</td>");
                writer.write("<td>");
                writer.write(list.get(i).getName());
                writer.write("</td>");
                writer.write("<td>");
                writer.write(list.get(i).getSex());
                writer.write("</td>");
                writer.write("<td>"+list.get(i).getAge()+"</td>");
                writer.write("<td>");
                writer.write(list.get(i).getMajor());
                writer.write("</td>");
                writer.write("<td>");
                writer.write(list.get(i).getTime());
                writer.write("</td>");
                writer.write("<td><a onclick=\"if(confirm('确认删除吗？')) return true;return false;\" methon='post' href=delOne?id="+list.get(i).getId()+">删除<a/></td>");
                writer.write("<td><a href=updateOne?id="+list.get(i).getId()+">更新<a/></td>");
                writer.write("</tr>");
            }

            writer.write("</table>");
            writer.write("</body>");
            writer.write("</html>");
        }

}
